package com.googlecode.quillen.domain;

import java.io.Serializable;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: greg
 * Date: Dec 2, 2008
 * Time: 9:48:32 PM
 */
public class ShadowFile implements Serializable
{
    private String key;
    private long size;
    private Date date;
    private List<String> chunkKeys;
    private Set<String> originalFilenames;
    private Set<String> filenames;

    public ShadowFile(String key)
    {
        this(key, null, null, 0, new ArrayList<String>());
    }

    public ShadowFile(String key, long size)
    {
        this(key, null, null, size, new ArrayList<String>());
    }

    public ShadowFile(String key, Date date)
    {
        this(key, null, date, 0, new ArrayList<String>());
    }

    public ShadowFile(String key, long size, Date date)
    {
        this(key, null, date, size, new ArrayList<String>());
    }

    public ShadowFile(String key, String filename, Date date)
    {
        this(key, filename, date, 0, new ArrayList<String>());
    }

    public ShadowFile(String key, String filename, Date date, long size)
    {
        this(key, filename, date, size, new ArrayList<String>());
    }

    private ShadowFile(String key, String filename, Date date, long size, List<String> chunkKeys)
    {
        this.key = key;
        this.size = size;
        this.date = date;
        this.chunkKeys = chunkKeys;

        originalFilenames = null;
        filenames = new HashSet<String>();

        if (filename != null)
        {
            addFile(filename);
        }
    }

    public static ShadowFile fromBytes(byte[] bytes)
    {
        String[] lines = new String(bytes).split("\n");

        return new ShadowFile(lines[0], null, null, Long.valueOf(lines[1]), Arrays.asList(lines).subList(2, lines.length));
    }

    public byte[] getBytes()
    {
        StringBuffer sb = new StringBuffer();

        sb.append(key).append("\n");
        sb.append(size).append("\n");

        for (String chunkKey : chunkKeys)
        {
            sb.append(chunkKey).append("\n");
        }

        return sb.toString().getBytes();
    }

    public void addChunk(Chunk chunk)
    {
        addChunk(chunk.getKey(), chunk.getSize());
    }

    public void addChunk(String key, int size)
    {
        chunkKeys.add(key);
        this.size += size;
    }

    public void addFile(String filename)
    {
        filenames.add(filename);
    }

    public String getKey()
    {
        return this.key;
    }

    public long getSize()
    {
        return this.size;
    }

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }

    public List<String> getChunkKeys()
    {
        return chunkKeys;
    }

    public Set<String> getOriginalFilenames()
    {
        return originalFilenames;
    }

    public void setOriginalFilenames(Set<String> originalFilenames)
    {
        this.originalFilenames = originalFilenames;
    }

    public Set<String> getFilenames()
    {
        return filenames;
    }
}